package com.lbssoft.gpsbd.dao;

import com.lbssoft.gpsbd.model.DeviceType;
import com.lbssoft.gpsbd.model.Role;
import com.lbssoft.gpsbd.model.vo.RoleVo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface RoleMapper {
	
	/**
	 * 获取角色列表
	 * @return
	 */
	@Select("SELECT * FROM lbs_roles")
	List<RoleVo> list();

	/**
	 * 添加角色
	 * @param role
     */
	@Insert("INSERT INTO lbs_roles(name,roleKey,groupId) VALUE(#{name},#{roleKey},#{groupId})")
	@Options(useGeneratedKeys=true, keyProperty="id")
	void add(Role role);

	/**
	 * 删除角色
	 * @param id
     */
	@Delete("DELETE FROM lbs_roles where id = #{id}")
	void delete(@Param("id") int id);

	/**
	 * 添加角色的权限关联关系
	 * @param roleId
	 * @param permissionId
     */
	@Insert("INSERT INTO lbs_role_permission(roleId,permissionId) VALUE(#{roleId},#{permissionId})")
	void addRolePermis(@Param("roleId") int roleId,@Param("permissionId") int permissionId);

	/**
	 * 修改角色信息
	 * @param role
     */
	@Update("UPDATE lbs_roles SET name = #{name},roleKey=#{roleKey} where id = #{id}")
	void update(RoleVo role);

	/**
	 * 删除角色的权限
	 * @param roleId
	 * @param permissionIds
	 */
	@Delete("DELETE from lbs_role_permission where roleId = #{roleId} AND permissionId in (${permissionIds})")
	void deleteRolePermis(@Param("roleId") int roleId,@Param("permissionIds") String permissionIds);

	/**
	 * 检查角色是否被使用
	 * @param roleId
	 * @return
     */
	@Select("SELECT * FROM lbs_user_role where roleId = #{roleId}")
	List<Map> getUserByRole(@Param("roleId") int roleId);

	/**
	 * 根据id获取角色信息
	 * @param id
     */
	@Select("SELECT * FROM lbs_roles where id = #{id}")
	Role getRoleById(@Param("id") int id);

	/**
	 * 根据Key获取角色信息
	 * @param roleKey
	 */
	@Select("SELECT * FROM lbs_roles where roleKey = #{roleKey}")
	Role getRoleByKey(@Param("roleKey") String roleKey);


	
	
}
